package com.yazi.mxz.person.controller.shop;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Sort;
import com.myframework.smhj.jackson.result.ListResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.entity.beans.MemberPrepaid;
import com.yazi.mxz.common.entity.enums.MemberPrepaidType;
import com.yazi.mxz.common.service.MemberPrepaidService;
import com.yazi.mxz.common.service.MemberService;
import com.yazi.mxz.common.service.MemberVipService;
import com.yazi.mxz.person.jackson.filter.MemberPrepaidFilter;

@Controller("shop-memberPrepaidController")
@RequestMapping("/shop/memberPrepaid")
public class MemberPrepaidController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(MemberPrepaidController.class);
	
	@Resource
	MemberService memberService;
	@Resource
	MemberVipService memberVipService;
	@Resource
	MemberPrepaidService memberPrepaidService;
	
	@RequestMapping("/list/json")
	@ResponseBody
	public ListResult<?> listJson(MemberPrepaid memberPrepaid, Searching si) {
		debug(log, "充值记录查询");
		ListResult<MemberPrepaid> jsonResult = new ListResult<MemberPrepaid>();
		jsonResult.setFilter(MemberPrepaidFilter.getWithMemberByCheckerId());
		Page<MemberPrepaid> page;
		try {
			// 只能查询在当前企业的充值记录
			memberPrepaid.setCompany(getCompany());
			// 默认按充值时间倒序排序
			if (null == si.getSorts()) {
				List<Sort> sorts = new ArrayList<Sort>();
				sorts.add(new Sort("addTime", true));
				si.setSorts(sorts);
			}
			page = memberPrepaidService.list(memberPrepaid, si);
			jsonResult.setResult(page.getList());
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "充值记录查询异常");
			jsonResult.setErrorMessage("充值记录查询失败,请联系管理员");
		}
		return jsonResult;
	}
	
	@RequestMapping("/add")
	public String add(String name, Model model) {
		debug(log, "进入会员充值页面");
		
		model.addAttribute("name", name);
		model.addAttribute("memPromote", getCompany(true).getMemPromote());
		return "shop/memberPrepaid_add";
	}
	
	@RequestMapping("/prepaidDo/json")
	@ResponseBody
	public StatusResult prepaidDoJson(String userName, Float prepaid) {
		debug(log, "会员充值提交");
		StatusResult jsonResult = new StatusResult();
		try {
			Member member = memberService.oneByAccountName(userName);
			if (null != member) {
				MemberPrepaid mp = new MemberPrepaid();
				mp.setMemberByMemberId(member);
				mp.setStore(getStore());
				mp.setCompany(getCompany());
				mp.setBalance(member.getBalance());
				// 现金充值
				mp.setType(MemberPrepaidType.CASH.getValue());
				mp.setPrepaid(prepaid);
				mp.setMemberByCheckerId(getMember());
				memberPrepaidService.add(mp);
				
				jsonResult.setOkMessage("充值成功");
				
			} else {
				jsonResult.setFailMessage("用户不存在");
			}
			
		} catch (MyException e) {
			debug(log, e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e ) {
			error(log, e, "会员充值提交时异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
}
